﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringDev"].ConnectionString;

        // Create SQL Command 

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select UserImage from Reg.UserDetails where UserID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        if (dReader["UserImage"] != DBNull.Value && !string.IsNullOrEmpty(Convert.ToString(dReader["UserImage"])))
            context.Response.BinaryWrite((byte[])dReader["UserImage"]);
        else
        { 
            context.Response.BinaryWrite(System.IO.File.ReadAllBytes(context.Server.MapPath("~/Classified/images/no-image.jpg")));
        }
        dReader.Close();
        con.Close();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}